package com.exartisan.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.exartisan.customview.GalleryViewPager;
import com.exartisan.customview.smartgalleryimageview.SmartGalleryView;

public class ImagePagerAdapter extends PagerAdapter {
	Context context;
	ArrayList<String> imgURLs;
	LayoutInflater inflater;
	protected int mCurrentPosition = -1;
	protected OnItemChangeListener mOnItemChangeListener;
	private ArrayList<Boolean> alLiked;

	public ImagePagerAdapter(Context context, ArrayList<String> imgURLs) {
		super();
		this.context = context;
		this.imgURLs = imgURLs;
		
		alLiked = new ArrayList<Boolean>();
		for (int i = 0; i < imgURLs.size(); i++) {
			alLiked.add(false);
		}
	}

	@Override
	public int getCount() {
		return imgURLs.size();
	}

	@Override
	public boolean isViewFromObject(View view, Object object) {
		return view == ((RelativeLayout) object);
	}

	@Override
	public void setPrimaryItem(ViewGroup container, int position, Object object) {
		super.setPrimaryItem(container, position, object);
		if (mCurrentPosition == position)
			return;
		GalleryViewPager galleryContainer = ((GalleryViewPager) container);
		if (galleryContainer.mCurrentView != null)
			galleryContainer.mCurrentView.resetScale();

		mCurrentPosition = position;
		if (mOnItemChangeListener != null)
			mOnItemChangeListener.onItemChange(mCurrentPosition);
		galleryContainer.mCurrentView = ((SmartGalleryView) object)
				.getImageView();
	}

	@Override
	public Object instantiateItem(ViewGroup container, final int position) {
		// Declare Variables
		SmartGalleryView smartImg;
		
		smartImg = new SmartGalleryView(context);

		// Set data
		smartImg.setUrl(imgURLs.get(position));
		smartImg.setId(position);

		// Add viewpager_item.xml to ViewPager
		((GalleryViewPager)container).addView(smartImg);

		return smartImg;

	}

	@Override
	public void destroyItem(View container, int position, Object object) {
		// TODO Auto-generated method stub
		((ViewPager) container).removeView((RelativeLayout) object);
	}

	public int getCurrentPosition() {
		return mCurrentPosition;
	}

	public void setOnItemChangeListener(OnItemChangeListener listener) {
		mOnItemChangeListener = listener;
	}

	public static interface OnItemChangeListener {
		public void onItemChange(int currentPosition);
	}
	
	public void setLiked(int position, boolean liked){
		alLiked.set(position, liked);
	}
	
	public boolean isLiked(int position){
		return alLiked.get(position);
	}

}
